package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean

public class CreateCustomLookupBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean addLookUpValue;
	private boolean FORM_VALID;
	private boolean LOOKUP_FORM_VALID;
	private CustomLookup customLookup = new CustomLookup();
	private LookupValue lookupValue = new LookupValue();
	CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	private List<LookupValue> lookupValues = new ArrayList<LookupValue>();   
	
	
	
	
		
	
	public void toggleAddLookupDisplay(ActionEvent e) {           
	
			System.out.println("toggleAddLookupDisplay");
			validateForm();
			if(FORM_VALID){
				setAddLookUpValue(true);	
			}
			
	}
	
	public void saveCustomLookup() {
		System.out.println("saveCustomLookup");
		validateForm();
		System.out.println("lookupValues.size()========="+lookupValues.size());
		
		if(lookupValues.size() == 0){
		     createAnotherLookupValueAction();
		}
		if (FORM_VALID  && lookupValues.size() >0) {			
			customLookup.setCreateDate(new Date());
			customLookup.setCreatedBy("DEVELOPER");
			customLookup.setStatus("ACTIVE");	
			customLookup.getLookupvalues().addAll(lookupValues);
			customLookupDAO.createCustomLookup(customLookup);
			setAddLookUpValue(false);
			FacesContext.getCurrentInstance().addMessage(
					null, new FacesMessage(FacesMessage.SEVERITY_INFO, customLookup.getCustomLookupName()
							+ " Custom lookup created successfully.", ""));
			customLookup = new CustomLookup();
			lookupValue = new LookupValue();
			lookupValues.clear();
		}
		
	}
	
	public String createAnotherLookupValueAction() {
		System.out.println("createAnotherResourceAction...............");
		anothervalidateForm();
		
		if (LOOKUP_FORM_VALID) {
			lookupValue.setCreateDate(new Date());
			lookupValue.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			lookupValue.setStatus(CommonLookup.STATUS_ACTIVE);           
			lookupValues.add(lookupValue);
			lookupValue = new LookupValue();
		}

		return null;
	}
	
	
	
	/*public String saveLookupValueAction() {
		
		
				
		try{
			if ((lookupValue.getLookupValue() == null || lookupValue.getLookupName().equals(""))) {
				customLookupDAO.createLookupValue(lookupValue);
			}

			if (lookupValues.size() == 0) {
				validateForm();
				if (FORM_VALID) {
				
				
					lookupValue.setCustomLookup(customLookup);
					customLookupDAO.createLookupValue(lookupValue);
					lookupValue = new LookupValue();
				}
			}
			lookupValues.clear();
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "LookupValue(s) created successfully.", ""));
		}catch (Exception e) {
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to create LookupValue" +
					".", e.getMessage()));
		}
	
		return null;
	
	    
	}*/
	
	
	
	public void validateForm(){
		FORM_VALID = true;
		if (ValidationUtil.isEmpty(customLookup.getCustomLookupName(), "Custom Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			FORM_VALID = false;
	    }
		
		if (ValidationUtil.isEmpty(customLookup.getDisplayName(), "Display Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Display Name is empty");
			FORM_VALID = false;
		}
   }	
	
	
	
	public void anothervalidateForm(){
		LOOKUP_FORM_VALID =true;
		if (ValidationUtil.isEmpty(lookupValue.getLookupValue(), "Lookup value",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			LOOKUP_FORM_VALID = false;
		}
		
		
		if (ValidationUtil.isEmpty(lookupValue.getLookupName(), "Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Lookup Name");
			LOOKUP_FORM_VALID = false;
		}
		
	}
	
	
	
/*	private void validateForm() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(customLookup.getCustomLookupName(), "Custom Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(customLookup.getDisplayName(), "Display Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Display Name is empty");
			FORM_VALID = false;
		}

		if (ValidationUtil.isEmpty(lookupValue.getLookupValue(), "Lookup value",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(lookupValue.getLookupName(), "Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Lookup Name");
			FORM_VALID = false;
		}

	}
	*/
	

	
	
	//Getter Setter Methods
	
	public boolean isAddLookUpValue() {
		return addLookUpValue;
	}

	public void setAddLookUpValue(boolean addLookUpValue) {
		this.addLookUpValue = addLookUpValue;
	}

	public CustomLookup getCustomLookup() {
		return customLookup;
	}

	public void setCustomLookup(CustomLookup customLookup) {
		this.customLookup = customLookup;
	}

	public LookupValue getLookupValue() {
		return lookupValue;
	}

	public void setLookupValue(LookupValue lookupValue) {
		this.lookupValue = lookupValue;
	}

	public List<LookupValue> getLookupValues() {
		return lookupValues;
	}

	public void setLookupValues(List<LookupValue> lookupValues) {
		this.lookupValues = lookupValues;
	}

	public boolean isLOOKUP_FORM_VALID() {
		return LOOKUP_FORM_VALID;
	}

	public void setLOOKUP_FORM_VALID(boolean lOOKUP_FORM_VALID) {
		LOOKUP_FORM_VALID = lOOKUP_FORM_VALID;
	}
}
